#include <cctype>
#include <cstdio>
#include <vector>

typedef unsigned long long ull;

template <typename T>
inline T &read(T &x) {
  x = 0;
  bool f = false;
  short ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = true;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getchar();
  if (f) x = -x;
  return x;
}

const int N = 1e6 + 5, mod = 1e9 + 7;
int n, dis[N], m;
ull dis2[N];
std::vector<int> G[2][N];
int q[N], tmp[N];
bool vis[N];

inline void add(int u, int v, int w) { G[w][u].push_back(v); }

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen64("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen64("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("path.in", "r", stdin);
  freopen("path.out", "w", stdout);
#endif
#endif

  read(n), read(m);
  for (int i = 1, u, v, w; i <= m; ++i) {
    read(u), read(v), read(w);
    add(u, v, w);
  }
  int l = 1, r = 1;
  q[1] = 1, vis[1] = true;
  while (l <= r) {
    int u = q[l++];
    for (int v : G[0][u])
      if (!vis[v]) vis[v] = true, q[++r] = v;
  }
  l = 1, r = 0;
  for (int i = 1; i <= n; ++i)
    if (vis[i]) q[++r] = i;
  while (l <= r) {
    int tot = 0;
    for (int i = l; i <= r && dis[q[i]] == dis[q[l]]; ++i) tmp[++tot] = q[i];
    l += tot;
    for (int i = 0; i < 2; ++i)
      for (int j = 1; j <= tot; ++j) {
        int u = tmp[j];
        for (int v : G[i][u])
          if (!vis[v]) {
            vis[v] = true;
            dis[v] = (((ull)dis[u] << 1) | i) % mod;
            q[++r] = v;
          }
      }
  }
  for (int i = 2; i <= n; ++i) printf("%d ", vis[i] ? dis[i] : -1);
  return 0;
}